﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using MyMes.Dto;
using MyMes.Infrastructure;
using MyMES.Dto;
using MyMES.IServices.ToolingFixtureLedger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Extensions;
using VOL.Core.Utilities.Response;
using VOL.Entity.DomainModels;
using VOL.Entity.DomainModels.Core;
using VOL.Entity.DomainModels.Repository;

namespace MyMES.Services.ToolingFixtureLedger
{
    public class ToolingFixtureLedgerService : IToolingFixtureLedgerService
    {
        private readonly IBaseRepository<toolingfixtureledger> _toolingfixtureledgerRepository;

        public ToolingFixtureLedgerService(IBaseRepository<toolingfixtureledger> toolingfixtureledgerRepository)
        {
            this._toolingfixtureledgerRepository = toolingfixtureledgerRepository;
        }
        /// <summary>
        /// 添加工装夹具台账信息
        /// </summary>
        /// <param name="toolingFixtureLedger">工装夹具台账实体</param>
        /// <returns></returns>
        public async Task<WebResponseContent> AddToolingfixtureledgerModel(toolingfixtureledger toolingFixtureLedger)
        {
            WebResponseContent response = new WebResponseContent();
            try
            {
                await _toolingfixtureledgerRepository.AddAsync(toolingFixtureLedger);
                return response.OK("添加工装夹具台账成功");
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除工装夹具台账信息
        /// </summary>
        /// <param name="toolingFixtureLedger">工装夹具台账实体</param>
        /// <returns>操作结果</returns>
        public async Task<WebResponseContent> DelToolingfixtureledgerModel(toolingfixtureledger toolingFixtureLedger)
        {
            WebResponseContent response = new WebResponseContent();
            try
            {

                await _toolingfixtureledgerRepository.Remove(toolingFixtureLedger);
                return response.OK("删除工装夹具台账成功");
            }
            catch (Exception ex)
            {
                return response.Error($"删除工装夹具台账失败：{ex.Message}");
            }
        }

        public async Task<PageGridData<toolingfixtureledger>> GetToolingfixtureledgers(string? code, string? name, string? type, int? isStatus, int pageIndex = 1, int pageSize = 5)
        {
            var pageData = new PageGridData<toolingfixtureledger>();
            var query = _toolingfixtureledgerRepository.GetAll();

            if (!string.IsNullOrEmpty(code))
                query = query.Where(x => x.Code == code);
            if (!string.IsNullOrEmpty(name))
                query = query.Where(x => x.Name.Contains(name));
            if (!string.IsNullOrEmpty(type))
                query = query.Where(x => x.Type == type);
            if (isStatus != null && isStatus != 0)
                query = query.Where(x => x.IsStatus == isStatus);

            pageData.total = query.Count();



            pageData.rows = await query.OrderByDescending(x => x.Id)
                             .Skip((pageIndex - 1) * pageSize).Take(pageSize)
                                  .ToListAsync();
            return pageData;
        }

        /// <summary>
        /// 修改工装夹具台账信息
        /// </summary>
        /// <param name="toolingFixtureLedger">工装夹具台账实体</param>
        /// <returns></returns>
        public async Task<WebResponseContent> UpdateToolingfixtureledgerModel(toolingfixtureledger toolingFixtureLedger)
        {
            WebResponseContent response = new WebResponseContent();
            try
            {
                await _toolingfixtureledgerRepository.UpdateAsync(toolingFixtureLedger);
                return response.OK("更新工装夹具台账成功");
            }
            catch (Exception ex)
            {
                return response.Error($"更新工装夹具台账失败：{ex.Message}");
            }
        }
    }
}
